﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Collections;

namespace 字符频率统计
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string str;
        System.Collections.ArrayList ar = new System.Collections.ArrayList();
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamReader ob = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.GetEncoding("gb2312"));
                while (ob.Peek() >= 0)
                {
                 ar.Add(ob.ReadToEnd());

                }
                ob.Close(); ob.Dispose();
            }
        

       
            for (int a = 0; a < ar.Count; a++)
            {
                str = ar[a].ToString();
            }

                textBox1.Text = str; 

            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            char[] cr = str.ToCharArray();
           
            foreach(char ch in cr)
            {
                if (ht.ContainsKey(ch))
                {
                    int i = (int)ht[ch];
                    ht.Remove(ch);
                    ht.Add(ch, ++i);
                }
                else
                { ht.Add(ch, 1); }
     
                }
            foreach(DictionaryEntry dicEntry in ht)
            {
                Console.WriteLine("{0}出现{1}次",dicEntry.Key ,dicEntry.Value);

            }
            System.Collections.ArrayList alist = new System.Collections.ArrayList(ht.Values);
            alist.Sort();
            object o = (object)alist[alist.Count - 1];
            foreach (DictionaryEntry dicEntry in ht)
            {
                if (dicEntry.Value == o)
                {
                    string st = dicEntry.Key.ToString();
                    Console.WriteLine("出现次数最多的字符:{0}，共{1}次", st, o.ToString());
                    break;
                }
            }
                Console.ReadLine();

            }
        }
        
     
    }

               